package com.newgrand.annexserver.storage.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.newgrand.annexcommon.constant.StatusConst;
import com.newgrand.annexcommon.model.FileEntity;
import com.newgrand.annexcommon.utils.NoUtil;
import com.newgrand.annexserver.beans.Attachment;
import com.newgrand.annexserver.beans.Attachmentpicthumb;
import com.newgrand.annexserver.beans.Dbconfig;
import com.newgrand.annexserver.context.ApplicationContextHolder;
import com.newgrand.annexserver.datasource.NgDataSourceContextHolder;
import com.newgrand.annexserver.mapper.store.AttachmentMapper;
import com.newgrand.annexserver.mapper.store.AttachmentpicthumbMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;


public abstract class RelationalStorage implements StorageService {

    private final static Logger logger = LoggerFactory.getLogger(RelationalStorage.class);

    private AttachmentMapper attachmentMapper;

    private AttachmentpicthumbMapper attachmentpicthumbMapper;

    private Dbconfig dbconfig;

    public RelationalStorage(Dbconfig dbconfig) {
        if (attachmentMapper == null) {
            attachmentMapper = ApplicationContextHolder.getBean(AttachmentMapper.class);
        }
        if (attachmentpicthumbMapper == null) {
            attachmentpicthumbMapper = ApplicationContextHolder.getBean(AttachmentpicthumbMapper.class);
        }
        //切换数据源
        this.dbconfig = dbconfig;
        //切换存储数据源
        NgDataSourceContextHolder.setBusinessDataSourceKey(dbconfig);
    }

    public String save(FileEntity entity) {
        try {
            String asr_fid = NoUtil.getAsrFid();
            Attachment at = new Attachment();
            at.setAsrFid(asr_fid);
            at.setAsrSeq(entity.getChunk());
            at.setAsrData(entity.getData());
            at.setAsrSize(entity.getData().length);
            at.setAsrCount(entity.getChunks());
            at.setAsrType(entity.isImg() == true ? 1 : 0);
            at.setCreatetime(new Date());
            at.setState(StatusConst.STATE_ENALBE);
            attachmentMapper.insert(at);
            return asr_fid;
        } catch (Exception e) {
            logger.error("save relational " + NgDataSourceContextHolder.getDataSourceKey(), e);
            return "";
        }
    }

    public String saveThumb(FileEntity entity) {
        try {
            String asr_fid = NoUtil.getAsrFid();
            Attachmentpicthumb at = new Attachmentpicthumb();
            String fileName = entity.getFileName();
            at.setAsrFid(asr_fid);
            at.setpAsrFid(entity.getP_asr_fid());
            at.setPicData(entity.getData());
            at.setPicSize(entity.getData().length);
            at.setCreatetime(new Date());
            at.setState(StatusConst.STATE_ENALBE);
            attachmentpicthumbMapper.insert(at);
            return asr_fid;
        } catch (Exception e) {
            logger.error("save thumb " + NgDataSourceContextHolder.getDataSourceKey(), e);
            return "";
        }
    }

    public FileEntity query(String asrfid) {
        logger.info("query {}", asrfid);
        try {
            List<Attachment> attachmentList = attachmentMapper.selectList(new EntityWrapper<Attachment>().ge("asr_fid", asrfid));
            if (attachmentList != null && attachmentList.size() > 0) {
                return getFileData(attachmentList.get(0));
            }
            return null;
        } catch (Exception e) {
            logger.error("query " + asrfid, e);
            return null;
        }
    }

    public FileEntity queryThumb(String p_asr_fid) {
        logger.info("queryThumb {}", p_asr_fid);
        try {
            List<Attachmentpicthumb> attachmentpicthumbList = attachmentpicthumbMapper.selectList(new EntityWrapper<Attachmentpicthumb>().ge("p_asr_fid", p_asr_fid));
            if (attachmentpicthumbList != null && attachmentpicthumbList.size() > 0) {
                return getThumbData(attachmentpicthumbList.get(0));
            }
            return null;
        } catch (Exception e) {
            logger.error("query thumb" + p_asr_fid, e);
            return null;
        }
    }


    private FileEntity getFileData(Attachment attachment) {
        FileEntity entity = new FileEntity();
        entity.setChunk(attachment.getAsrSeq());
        entity.setData(attachment.getAsrData());
        entity.setImg(attachment.getAsrType() == 1 ? true : false);
        entity.setChunks(attachment.getAsrCount());
        return entity;
    }

    private FileEntity getThumbData(Attachmentpicthumb attachmentpicthumb) {
        FileEntity entity = new FileEntity();
        entity.setP_asr_fid(attachmentpicthumb.getpAsrFid());
        entity.setData(attachmentpicthumb.getPicData());
        return entity;
    }

    public void close() {

    }


}
